<template>
	<view v-if="popShow">
		<tn-popup v-model="show" @close="handlePopShow(false)"  mode="center" length="90%">
		  <view>
			  <view class="flex padding">
			  	<view class="flex align-center">
			  			<image class="poster margin-right" :src="popData.img" mode=""></image>
			  		</view>
			  		<view class="flex justify-between info list">
			  			<view class="">
			  				{{popData.name}}
			  				<view class="margin-tb-xs">{{popData.french}}</view>
			  				￥{{popData.list[dropIdx].price}}
			  			</view>
			  		</view>
			  </view>
			  <view class="flex padding justify-between">
			  	规格选择
				<view class="drop" @click="dropShow=true">
					{{dropCheckInfo}}
					<view v-if="dropShow" class="drop-list">
						<view v-for="(itm,idx) in popData.list"  @click.stop="handleDrop(idx)" class="">
							{{itm.spec}}({{itm.weight}})-{{itm.edible}}
						</view>
					</view>
				</view>
			  </view>
			  <view class="flex padding justify-between">
			  	规格数量
			  	<tn-number-box v-model="num" :min="1" backgroundColor="#FFE32A"></tn-number-box>
			  </view>
			  <view class="flex text-center">
				  <view class="my-btn padding bg-brown">
				  	取消
				  </view>
				  <view @click="handleAdd" class="my-btn padding bg-yellow">
				  	确认
				  </view>
			  </view>
		  </view>
		</tn-popup>
	</view>
</template>

<script>
	import {mapState,mapMutations} from 'vuex'
	export default {
		name:"cake-pop",
		data() {
			return {
				show:true,
				dropShow:false,
				dropIdx:0, //选中的规格序号
				num:1,   //数量
			};
		},
		computed: {
			...mapState({
				popShow:state=>state.cart.popShow,
				popData:state=>state.cart.popData,
				editIdx:state=>state.cart.editIdx
			}),
			dropCheckInfo(){
				let {dropIdx,popData} = this
				let {spec,weight,edible} = popData.list[dropIdx]
				return `${spec}(${weight})-${edible}`
			}
		},
		watch:{
			popShow(newVal){
				this.show = newVal
				if(newVal&&this.editIdx!=-1){
					let {dropIdx,num} = this.popData
					this.dropIdx = dropIdx
					this.num = num
				}else{
					this.dropIdx = 0
					this.num = 1
				}
 			}
		},
		methods: {
			...mapMutations({
				handlePopShow:'cart/popShowMut'
			}),
			handleDrop(idx){
				this.dropShow = false
				this.dropIdx = idx
			},
			handleAdd(){
				let {dropIdx,popData,num} = this
				this.$store.commit('cart/cartAddMut',{
					...popData, //商品数据
					dropIdx, //选中的规格
					num //数量
				})
				this.$store.commit('cart/popShowMut',false)
			}
		},
	}
</script>

<style lang="scss">
.poster{
	width: 180upx;
	height: 180upx;
	background-color: #d8d8d8;
}
.info{
	&.list{
		width: 60%;
	}
	.edit{
		width: 80upx;
		height: 80upx;
		text-align: center;
		line-height: 80upx;
		background-color: #e6e6e6;
		border-radius: 50%;
		
	}
}
.flex.align-end{
	min-width: 210upx;
}
.icon-gouxuan{
	color: #e7e7e7;
}
.cu-btn.lg{
	width: 50%;
}
.cover-cont{
	position: absolute;
	top: 50%;
	left: 0;
	width: 690upx;
	transform: translateY(-50%);
	border-radius: 10upx;
}
.drop{
	position: relative;
	.drop-list{
		width: 300upx;
		position: absolute;
		top: 60upx;
		right: 0;
		box-shadow: 0 0 10upx 2upx rgba(0,0,0,0.2);
		z-index: 10;
		view:hover{
			background-color: #e6e6e6;
		}
		background-color: #fff;
	}
}

.my-btn{
	width: 50%;
}
</style>